package com.feihong.yw189.bluetooth.utils.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.feihong.yw189.bluetooth.listener.InitHodListener;
import com.sunnada.SYDReader.ConsantHelper;
import com.sunnada.SYDReader.IdentityCardS;

public class Busi_Idcard_device {

    private String TIP = "提示";
    private static final int MSG_REG_FAIL = 0x02;
    private static final int MSG_CHECK_ID_SUC = 0x03;
    public static final int MSG_CHECK_ID_FAIL = 0x04;
    private static final int MSG_CHECK_ID_TIMEOUT = 0x05;
    private static final int MSG_GET_ID_INFO_SUCC = 0x06;
    private static final int MSG_GET_ID_INFO_FAIL = 0x07;

    protected Context main_activity;
    protected Activity activity;
    protected Dialog dialog;

    protected boolean isClosed = false;
    public boolean isFail = false;
    public boolean getResult = false;
    private CallBack callBack;     //读取成功的回调函数
    public String bmpPath;
    public boolean continueflag = false;   //
    private InitHodListener mInitHodListener;


    public Busi_Idcard_device(Context a, InitHodListener mInitHodListener) {
        this.main_activity = a;
        this.activity = (Activity) a;
        this.mInitHodListener = mInitHodListener;
    }

    public Busi_Idcard_device(Context a) {
        this.main_activity = a;
        this.activity = (Activity) a;
    }

    @SuppressLint("HandlerLeak")
    public Handler BmHanddler = new Handler() {
        public void handleMessage(Message msg) {
            isClosed = true;
            switch (msg.what) {

                // 身份证识别失败
                case MSG_REG_FAIL:
                    mInitHodListener.initHod("识别失败！");

                    break;
                // 验证身份证信息失败
                case MSG_CHECK_ID_FAIL:
                    String desc = (String) msg.obj;
                    if (TextUtils.isEmpty(desc) || "anyType{}".equals(desc)) {
                        desc = "读取失败";
                    }

                    mInitHodListener.initHod(desc);
                    break;

                //验证身份证信息超时
                case MSG_CHECK_ID_TIMEOUT:

                    mInitHodListener.initHod("身份验证超时");
                    break;
                case ConsantHelper.READ_IDCARD_SUCC:
                case MSG_GET_ID_INFO_SUCC: {

                    isFail = false;
                    IdentityCardS mid = (IdentityCardS) (msg.obj);
                    callBack.HandleResult(mid, null, null);
                }
                break;
                case ConsantHelper.READER_NOT_FOUND_INDEX:
                case ConsantHelper.READER_ONLY_MATCH_ONE_PHONE:
                case ConsantHelper.NETWORK_UNAVAILABLE:

                case ConsantHelper.IDCARD_NOT_FOUND:
                case ConsantHelper.SERVER_ERROR:
                case ConsantHelper.READER_ERROR:

                case ConsantHelper.SOMETHING_WRONG_NEED_RETRY:
                case ConsantHelper.READ_IDCARD_INFO_ERROR:
                case ConsantHelper.READ_IDCARD_CANNOT_CONNECT_TO_SERVER:

                case ConsantHelper.CONNET_TO_SERVER_TIMEOUT:
                case ConsantHelper.CONNECT_TO_SERVER_ERROR:
                case ConsantHelper.SERVER_BUSY_ERROR: {
                    if (isFail) {
                        break;
                    }
                    isFail = true;
                    String err_type = (String) msg.obj;
                    mInitHodListener.initHod(err_type + "");
                }
                break;
                case MSG_GET_ID_INFO_FAIL:
                    String err_type = (String) msg.obj;
                    mInitHodListener.initHod(err_type + "");
                    break;

            }
        }
    };

    public void setIdCardCallback(CallBack m) {
        callBack = m;
    }
}
